package com.dycx.system.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.common.enums.SourceEnum;
import com.dycx.system.dto.LogDTO;
import com.dycx.system.entity.LogDO;
import com.dycx.system.vo.LogVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统日志(Log)实体转换类
 *
 * @author wuzongbing
 * @since 2025-01-13 16:13:17
 */


@Data
public class LogConverter implements Serializable {
    public static LogDO dtoToEntity(LogDTO dto) {
        if (dto == null) return null;
        LogDO entity = BeanUtil.toBean(dto, LogDO.class);
        return entity;
    }

    public static LogVO entityToVo(LogDO entity) {
        if (entity == null) return null;
        LogVO vo = BeanUtil.toBean(entity, LogVO.class);
        vo.setBizTypeName(BizTypeEnum.converter(vo.getBizType()));
        vo.setSourceName(SourceEnum.converter(vo.getSource()));
        return vo;
    }

    public static List<LogVO> entityToVoList(List<LogDO> entityList) {
        List<LogVO> voList = new ArrayList<>();
        for (LogDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<LogVO> entityToVoPage(Page<LogDO> page) {

        List<LogVO> newRecords = new ArrayList<>();

        List<LogDO> records = page.getRecords();
        for (LogDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<LogVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
